// $Id: logrecom.js,v 1.20 2010/12/04 05:03:50 jonghoon_kang Exp $
(function(){
	var $LR = {
		Config:{
			host:'log9.recommender.jp'
			,debug:false
			,compatible:true
			,strict:false
			,noImage:'http://noimage'
		}
		,imgInsure:function(img){
			img.onerror = null;
			img.src = $LR.Config.noImage;
		}
		,getCookie:function(name){
			var start = document.cookie.indexOf(name + "=");
			var len = start + name.length + 1;
			if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
			if (start == -1) return null;
			var end = document.cookie.indexOf(";",len);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(len, end));
		}
		,setSessionCookie:function(name, value) {
			document.cookie = name + "=" + escape(value) + "; path=/";
		}
		,newSessionID:function() {
			var ssid = [];
			while (ssid.join('').length < 40) {
				ssid.push(parseInt(('' + Math.random()).substring(2), 10).toString(36));
			}
			return ssid.join('');
		}
		,getSessionID:function() {
			var lrid = $LR.getCookie("lrsessionid");
			if (!lrid) {
				lrid = $LR.newSessionID();
				$LR.setSessionCookie("lrsessionid", lrid)
			}
			return lrid;
		}
		,refEscape:function(ref) {
			return /[^\w\!\'\(\)\*\-\.\~\%]/.test(ref) ? encodeURIComponent(ref) : ref;
		}
		,createActionPath:function(optionParam, type_id){
			optionParam = optionParam || '';
			type_id = type_id || $LR.Config.type_id;
			return [
				$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), '&lrtype=rep&id=', type_id, optionParam,
			].join('')
		}
		,createScript:function(src,rootNode){
			var s = document.createElement('script');
			s.type = "text/javascript";
			s.charset="UTF-8";
			s.src= src;
			rootNode.appendChild(s);
		}
		,register:function(lr_client_id){
			$LR.Config.lr_client_id = lr_client_id;
			$LR.Config.basePath = [location.protocol, '//', $LR.Config.host, '/', $LR.Config.lr_client_id].join('');
			$LR.registered = $LR.register;
		}
		,getClickReferrer:function(){
			var lrReferrer = $LR.refEscape(document.referrer);
			return lrReferrer;
		}
		,getBaseParam:function(){
			return ['?lrid=', $LR.getSessionID(), '&d=', (new Date()).getTime(), '&lrref=', $LR.getClickReferrer()].join('');
		}
		,escapeUnicode:function(str) {
			return str.replace(/[^ -~]|\\/g, function(m0) {
				var code = m0.charCodeAt(0);
				return '\\u' + (
								(code < 0x10)? '000' :
								(code < 0x100)? '00' :
								(code < 0x1000)? '0' :
								''
							) + code.toString(16);
				});
		}
		,type_mix:function(lr_client_id, ref){
			ref = ref ? '&ref=' + $LR.refEscape(ref) : '';
			$LR.register(lr_client_id);
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), ref, '&lrtype=mix'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
		,type_view:function(lr_client_id, ref, id) {
			$LR.register(lr_client_id);
			ref = ref ? '&ref=' + $LR.refEscape(ref) : '';
			id = id ? '&id=' + id : '';
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), ref, id, '&lrtype=view'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
		,type_hybrid:function(lr_client_id, ref, id) {
			$LR.register(lr_client_id);
			ref = ref ? '&ref=' + $LR.refEscape(ref) : '';
			id = id ? '&id=' + id : '';
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), ref, id, '&lrtype=hybrid'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
		,type_log:function(lr_client_id, id, ref) {
			$LR.register(lr_client_id);
			ref = ref ? '&ref=' + $LR.refEscape(ref) : '';
			id = id ? '&id=' + id : '';
			var img = new Image(1,1);
			img.src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), id, ref, '&lrtype=log'].join("");
		}
		,clickCartLog:function(lr_client_id, id, ref,event,anchor) {
			$LR.register(lr_client_id);
			ref = ref ? '&ref=' + $LR.refEscape(ref) : '';
			id = id ? '&id=' + id : '';
			var actionPath = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), id, ref, '&lrtype=log'].join('');
			if (window.Components) {
				if (event && event.preventDefault && anchor && anchor.href) {
					event.preventDefault();
					var href = anchor.href;
					setTimeout(function(){
						location.href = href;
					},1000);
				}
			}
			var img = new Image(1,1);
			img.src = actionPath;
			img.onload = function() {
				if (window.Components && typeof href == 'string' && !$LR.Config.debug) {
					location.href = href;
				}
			}
		}
		,click_log:function(click_id, type_id, event, anchor) {
			if (window.Components) {
				if (event && event.preventDefault && anchor && anchor.href) {
					event.preventDefault();
					var href = anchor.href;
					setTimeout(function(){
						location.href = href;
						//console.log(href,click_id);
					},1000);
				}
			}
			var optionParam = '&cid=' + click_id;
			var actionPath = $LR.createActionPath(optionParam, type_id);
			var img = new Image(1,1);
			img.src = actionPath;
			img.onload = function() {
				if (window.Components && typeof href == 'string' && !$LR.Config.debug) {
					location.href = href;
					//console.log(href,click_id);
				}
			}
		}
		,reportLog:function(type_id) {
			$LR.Config.type_id = type_id;
			var optionParam = '';
			var actionPath = $LR.createActionPath(optionParam, type_id);
			var img = new Image(1,1);
			img.src = actionPath;
			img.onload=function() { lrVoid(); }
		}
		,type_multiple:function(lr_client_id,ref) {
			$LR.register(lr_client_id);
			ref =  ref ? '&ref=' + $LR.refEscape(ref) : '';
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), ref, '&lrtype=multipleType'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
		,type_history:function(lr_client_id,ref,id) {
			$LR.register(lr_client_id);
			ref =  ref ? '&ref=' + $LR.refEscape(ref) : '';
			id = id ? '&id=' + id : '';
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), ref,id, '&lrtype=history'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
		,type_additional:function(lr_client_id,ref,id) {
			$LR.register(lr_client_id);
			ref = ref ? '&ref=' + $LR.refEscape(ref) : '';
			id = id ? '&id=' + id : '';
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), ref, id, '&lrtype=additional'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
		,type_ranking:function(lr_client_id,category_id,id) {
			$LR.register(lr_client_id);
			category_id = category_id ? '&ref=' + $LR.refEscape($LR.escapeUnicode(category_id)) : '';
			id = id ? '&id=' + id : '';
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), category_id, id, '&lrtype=ranking'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
		,type_multiple2:function(lr_client_id,ref,id) {
			$LR.register(lr_client_id);
			ref =  ref ? '&ref=' + $LR.refEscape(ref) : '';
			id = id ? '&id=' + id : '';
			var src = [$LR.Config.basePath, '/logrecom.html', $LR.getBaseParam(), ref, id, '&lrtype=multipleType2'].join('');
			$LR.createScript(src,document.getElementById("logrecom"));
		}
	}
	window.LogRecommender = window.LogRecommender || $LR;

	if ($LR.Config.compatible) {
		window.getCookie = $LR.getCookie;
	}
	if (!$LR.Config.strict) {
		window.logrecom_reportLog     = $LR.reportLog;
		window.logrecom_type_mix      = $LR.type_mix;
		window.logrecom_type_view     = $LR.type_view;
		window.logrecom_type_hybrid     = $LR.type_hybrid;
		window.logrecom_type_log      = $LR.type_log;
		window.logrecom_type_multiple = $LR.type_multiple;
		window.logrecom_cart_log      = $LR.clickCartLog;
		window.logrecom_click_log     = $LR.click_log;
		window.logrecom_type_history  = $LR.type_history;
		window.logrecom_type_additional  = $LR.type_additional;
		window.logrecom_type_ranking  = $LR.type_ranking;
		window.logrecom_type_multiple2 = $LR.type_multiple2;
	}
	function lrVoid(){return;}
})();
